package edu.unc.comp410.colemangeer.data;

import static java.lang.System.out;
import edu.unc.comp410.colemangeer.model.PersistentObject;

public class Zip extends PersistentObject {
	// Constants
	private static final String SEP = ","; // Field separator in String
											// representation.
	private static final String REGEX_SEP = SEP; // = "\\" + SEP;

	// Zip info
	private static final int NUM_FIELDS = 3;
	private String zipCode;
	private String city;
	private String stateAbbrev;

	public Zip(String theZipCode, String theCity, String theStateAbbrev) {
		super();
		zipCode = theZipCode;
		city = theCity;
		stateAbbrev = theStateAbbrev;
	}

	@Override
	public Object clone() {
		return new Zip(zipCode, city, stateAbbrev);
	}

	/**
	 * Convert a String representation into a Customer NOTE this converts the DB
	 * input file but not a toString() output from Zip.
	 */
	public static Zip string2Object(String str) {
		String[] field = str.split(REGEX_SEP);
		if (5 != field.length)
			throw new IllegalArgumentException();
		// Integer theId = Integer.parseInt( field[0] );
		return new Zip(field[0], field[3], field[4]);
	}

	/**
	 * Note - 2 blank fields added for consistency with the DB.in file.
	 */
	@Override
	public String toString() {
		String result = getZipCode() + SEP + SEP + SEP + getCity() + SEP + getStateAbbrev();
		return result;
	}

	/**
	 * @return the zipCode
	 */
	public String getZipCode() {
		return zipCode;
	}

	/**
	 * @param zipCode
	 *            the zipCode to set
	 */
	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}

	/**
	 * @return the city
	 */
	public String getCity() {
		return city;
	}

	/**
	 * @param city
	 *            the city to set
	 */
	public void setCity(String city) {
		this.city = city;
	}

	/**
	 * @return the stateAbbrev
	 */
	public String getStateAbbrev() {
		return stateAbbrev;
	}

	/**
	 * @param stateAbbrev
	 *            the stateAbbrev to set
	 */
	public void setStateAbbrev(String stateAbbrev) {
		this.stateAbbrev = stateAbbrev;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		// int result = super.hashCode();
		int result = 17;
		result = prime * result + ((city == null) ? 0 : city.hashCode());
		result = prime * result
				+ ((stateAbbrev == null) ? 0 : stateAbbrev.hashCode());
		result = prime * result + ((zipCode == null) ? 0 : zipCode.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		// if (!super.equals(obj))
		// return false;
		if (!(obj instanceof Zip))
			return false;
		final Zip other = (Zip) obj;
		if (city == null) {
			if (other.city != null)
				return false;
		} else if (!city.equals(other.city))
			return false;
		if (stateAbbrev == null) {
			if (other.stateAbbrev != null)
				return false;
		} else if (!stateAbbrev.equals(other.stateAbbrev))
			return false;
		if (zipCode == null) {
			if (other.zipCode != null)
				return false;
		} else if (!zipCode.equals(other.zipCode))
			return false;
		return true;
	}

}
